package com.nannybear.View;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.nannybear.R;

import java.util.ArrayList;

/**
 * \brief The health screen
 * \author Benjamin LANCINOT
 * \date 4-05-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class HealthScreen extends Screen {
    /********** DECLARATIONS **********/
    // Variables
    private Button add_button;
    private ImageButton age_up, age_down, unit_up, unit_down, changeCurve_button;
    private EditText data_text;
    private TextView data,data_unit, age, unit, title, warning_neutral;
    private GraphView graph;
    private Paint paint, paint2;
    private ArrayList<DataPoint> mWeight, mSize;
    private LineGraphSeries<DataPoint> serie;
    private ImageView baby_gender;
    private boolean isWeight;

    /************ METHODS ************/
    /**
     * \brief Constructor
     */
    public HealthScreen(){
        super(R.layout.health_screen,false,false);
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setColor(Color.rgb(155, 20, 20));
        paint.setPathEffect(new DashPathEffect(new float[]{9, 6}, 0));

        paint2 = new Paint();
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setStrokeWidth(3);
        paint2.setColor(Color.rgb(20, 155, 20));
        paint2.setPathEffect(new DashPathEffect(new float[]{9, 6}, 0));

        serie =new LineGraphSeries<>();
        serie.setDataPointsRadius(3);
        serie.setThickness(4);
        mWeight = new ArrayList<>();
    }

    /**
     * \brief Customize the axis graphs
     * \param unit  The unit
     */
    private void customMyGraphs(final String unit){
        graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
                @Override
                public String formatLabel(double value, boolean isValueX) {
                    //generate X-axis label
                    if (isValueX) {
                        int weeks = Double.valueOf(value % 4).intValue();
                        value = value / 4;
                        int months = Double.valueOf(value % 12).intValue();
                        value = value / 12;
                        int years = Double.valueOf(value).intValue();
                        if (years == 0) {
                            if (months == 0) {
                                if (weeks == 0) {
                                    return " ";
                                }
                                else {
                                    return super.formatLabel(weeks, true) + "w";
                                }

                            } else {
                                if (weeks == 0) {
                                    return super.formatLabel(months, true) + "m ";
                                }
                                else {
                                    return super.formatLabel(months, true) + "m " + super.formatLabel(weeks, true) + "w";
                                }
                            }
                        }
                        else {
                            if (months == 0) {
                                if (weeks == 0) {
                                    return super.formatLabel(years, true) + "y";
                                }
                                else {
                                    return super.formatLabel(years, true) + "y" + super.formatLabel(weeks, true) + "w ";
                                }

                            }
                            else {
                                if (weeks == 0) {
                                    return super.formatLabel(years, true) + "y" + super.formatLabel(months, true) + "m ";
                                }
                                else {
                                    return super.formatLabel(years, true) + "y" + super.formatLabel(months, true) + "m " + super.formatLabel(weeks, true) + "w";
                                }
                            }
                        }
                    }
                    //generate Y-axis label
                    else {
                        //Cast to one decimal digit
                        int tmp = (int)(value *10);
                        value = tmp/10.0;
                        return "    " + super.formatLabel(value, false) + unit;
                    }
                }
        });
    }

    /**
     * \brief Add a new data in the arrayList
     * \param age   The age value
     * \param data  The data
     */
    private void addDataInArrayList(int age, float data){
        int i=0;
        boolean run = true;
        if(isWeight) {
            while (run) {
                if (mWeight.get(i).getX() == age){
                    mWeight.set(i, new DataPoint(age, data));
                    run=false;
                }else if(mWeight.get(i).getX() < age && i != mWeight.size()-1){
                    i++;
                }else if (mWeight.get(i).getX() < age && i == mWeight.size()-1){
                    mWeight.add(i+1, new DataPoint(age, data));
                    run=false;
                }
                else{
                    mWeight.add(i, new DataPoint(age, data));
                    run=false;
                }
            }
            changeYAxis(true);
        }
        else{
            while (run) {
                if (mSize.get(i).getX() == age){
                    mSize.set(i, new DataPoint(age, data));
                    run=false;
                }else if(mSize.get(i).getX() < age && i != mSize.size()-1){
                    i++;
                }else if (mSize.get(i).getX() < age && i == mSize.size()-1){
                    mSize.add(i+1, new DataPoint(age, data));
                    run=false;
                }
                else{
                    mSize.add(i, new DataPoint(age, data));
                    run=false;
                }
            }
            changeYAxis(false);
        }
    }

    /**
     * \brief Redraw the view
     * \param age The age value
     * \param data  The data
     */
    public void addAData(int age, float data){
        addDataInArrayList(age,data);
        if(isWeight){
            serie.resetData(mWeight.toArray(new DataPoint[mWeight.size()]));

        }else{
            serie.resetData(mSize.toArray(new DataPoint[mSize.size()]));
        }
    }

    /**
     * \brief Change the Y axe to scale the view
     * \param isWeight  True if the user are on the weight curve
     */
    public void changeYAxis(boolean isWeight){
        graph.getViewport().setMinY(findMinValue(isWeight));
        graph.getViewport().setMaxY(findMaxValue(isWeight));
    }

    /**
     * \brief The low weight by gender
     * \param gender    The baby gender
     * \return The low weight values
     */
    private LineGraphSeries<DataPoint> getLowWeight(String gender) {
        if (gender.equals("girl")) {
            return new LineGraphSeries<>(new DataPoint[]{
                    new DataPoint(0, 2.0),
                    new DataPoint(1, 2.1),
                    new DataPoint(2, 2.3),
                    new DataPoint(3, 2.5),
                    new DataPoint(4, 2.7),
                    new DataPoint(5, 2.9),
                    new DataPoint(6, 3.0),
                    new DataPoint(7, 3.2),
                    new DataPoint(8, 3.3),
                    new DataPoint(9, 3.5),
                    new DataPoint(10, 3.7),
                    new DataPoint(11, 3.8),
                    new DataPoint(12, 3.9),
                    new DataPoint(13, 4.0),

                    new DataPoint(16, 4.4),
                    new DataPoint(20, 4.8),
                    new DataPoint(24, 5.1),
                    new DataPoint(28, 5.3),
                    new DataPoint(32, 5.6),
                    new DataPoint(36, 5.8),
                    new DataPoint(40, 5.9),
                    new DataPoint(44, 6.1),
                    new DataPoint(48, 6.3),
                    new DataPoint(52, 6.4),
                    new DataPoint(56, 6.6),
                    new DataPoint(60, 6.7),
                    new DataPoint(64, 6.9),
                    new DataPoint(68, 7.0),
                    new DataPoint(72, 7.2),
                    new DataPoint(76, 7.3),
                    new DataPoint(80, 7.5),
                    new DataPoint(84, 7.6),
                    new DataPoint(88, 7.8),
                    new DataPoint(92, 7.9),
                    new DataPoint(96, 8.1),
                    new DataPoint(100, 8.2),
                    new DataPoint(104, 8.4),
                    new DataPoint(108, 8.5),
                    new DataPoint(112, 8.6),
                    new DataPoint(116, 8.8),
                    new DataPoint(120, 8.9),
                    new DataPoint(124, 9.0),
                    new DataPoint(128, 9.1),
                    new DataPoint(132, 9.3),
                    new DataPoint(136, 9.4),
                    new DataPoint(140, 9.5),
                    new DataPoint(144, 9.6)
            });
        }
        else {
            return new LineGraphSeries<>(new DataPoint[]{
                    new DataPoint(0, 2.1),
                    new DataPoint(1, 2.2),
                    new DataPoint(2, 2.4),
                    new DataPoint(3, 2.6),
                    new DataPoint(4, 2.9),
                    new DataPoint(5, 3.1),
                    new DataPoint(6, 3.3),
                    new DataPoint(7, 3.5),
                    new DataPoint(8, 3.7),
                    new DataPoint(9, 3.8),
                    new DataPoint(10, 4.0),
                    new DataPoint(11, 4.2),
                    new DataPoint(12, 4.3),
                    new DataPoint(13, 4.4),

                    new DataPoint(16, 4.9),
                    new DataPoint(20, 5.3),
                    new DataPoint(24, 5.7),
                    new DataPoint(28, 5.9),
                    new DataPoint(32, 6.2),
                    new DataPoint(36, 6.4),
                    new DataPoint(40, 6.6),
                    new DataPoint(44, 6.8),
                    new DataPoint(48, 6.9),
                    new DataPoint(52, 7.1),
                    new DataPoint(56, 7.2),
                    new DataPoint(60, 7.4),
                    new DataPoint(64, 7.5),
                    new DataPoint(68, 7.7),
                    new DataPoint(72, 7.8),
                    new DataPoint(76, 8.0),
                    new DataPoint(80, 8.1),
                    new DataPoint(84, 8.2),
                    new DataPoint(88, 8.4),
                    new DataPoint(92, 8.5),
                    new DataPoint(96, 8.6),
                    new DataPoint(100, 8.8),
                    new DataPoint(104, 8.9),
                    new DataPoint(108, 9.0),
                    new DataPoint(112, 9.1),
                    new DataPoint(116, 9.2),
                    new DataPoint(120, 9.4),
                    new DataPoint(124, 9.5),
                    new DataPoint(128, 9.6),
                    new DataPoint(132, 9.7),
                    new DataPoint(136, 9.8),
                    new DataPoint(140, 9.9),
                    new DataPoint(144, 10.0)
            });
        }
    }

    /**
     * \brief The normal weight by gender
     * \param gender    The baby gender
     * \return The normal weight values
     */
    private LineGraphSeries<DataPoint> getNormalWeight(String gender){
        if (gender.equals("girl")) {
            return new LineGraphSeries<>(new DataPoint[]{
                    new DataPoint(0, 3.2),
                    new DataPoint(1, 3.3),
                    new DataPoint(2, 3.6),
                    new DataPoint(3, 3.8),
                    new DataPoint(4, 4.1),
                    new DataPoint(5, 4.3),
                    new DataPoint(6, 4.6),
                    new DataPoint(7, 4.8),
                    new DataPoint(8, 5.0),
                    new DataPoint(9, 5.2),
                    new DataPoint(10, 5.4),
                    new DataPoint(11, 5.5),
                    new DataPoint(12, 5.7),
                    new DataPoint(13, 5.8),

                    new DataPoint(16, 6.4),
                    new DataPoint(20, 6.9),
                    new DataPoint(24, 7.3),
                    new DataPoint(28, 7.6),
                    new DataPoint(32, 7.9),
                    new DataPoint(36, 8.2),
                    new DataPoint(40, 8.5),
                    new DataPoint(44, 8.7),
                    new DataPoint(48, 8.9),
                    new DataPoint(52, 9.2),
                    new DataPoint(56, 9.4),
                    new DataPoint(60, 9.6),
                    new DataPoint(64, 9.8),
                    new DataPoint(68, 10.0),
                    new DataPoint(72, 10.2),
                    new DataPoint(76, 10.4),
                    new DataPoint(80, 10.6),
                    new DataPoint(84, 10.9),
                    new DataPoint(88, 11.2),
                    new DataPoint(92, 11.3),
                    new DataPoint(96, 11.5),
                    new DataPoint(100, 11.7),
                    new DataPoint(104, 11.9),
                    new DataPoint(108, 12.1),
                    new DataPoint(112, 12.3),
                    new DataPoint(116, 12.5),
                    new DataPoint(120, 12.7),
                    new DataPoint(124, 12.9),
                    new DataPoint(128, 13.1),
                    new DataPoint(132, 13.3),
                    new DataPoint(136, 13.5),
                    new DataPoint(140, 13.7),
                    new DataPoint(144, 13.9)
            });
        }
        else {
            return new LineGraphSeries<>(new DataPoint[]{
                    new DataPoint(0, 3.3),
                    new DataPoint(1, 3.5),
                    new DataPoint(2, 3.8),
                    new DataPoint(3, 4.1),
                    new DataPoint(4, 4.4),
                    new DataPoint(5, 4.7),
                    new DataPoint(6, 4.9),
                    new DataPoint(7, 5.2),
                    new DataPoint(8, 5.4),
                    new DataPoint(9, 5.6),
                    new DataPoint(10, 5.8),
                    new DataPoint(11, 6.0),
                    new DataPoint(12, 6.2),
                    new DataPoint(13, 6.4),

                    new DataPoint(16, 7.0),
                    new DataPoint(20, 7.5),
                    new DataPoint(24, 7.9),
                    new DataPoint(28, 8.3),
                    new DataPoint(32, 8.6),
                    new DataPoint(36, 8.9),
                    new DataPoint(40, 9.2),
                    new DataPoint(44, 9.4),
                    new DataPoint(48, 9.6),
                    new DataPoint(52, 9.9),
                    new DataPoint(56, 10.1),
                    new DataPoint(60, 10.3),
                    new DataPoint(64, 10.5),
                    new DataPoint(68, 10.7),
                    new DataPoint(72, 10.9),
                    new DataPoint(76, 11.1),
                    new DataPoint(80, 11.3),
                    new DataPoint(84, 11.5),
                    new DataPoint(88, 11.8),
                    new DataPoint(92, 12.0),
                    new DataPoint(96, 12.2),
                    new DataPoint(100, 12.4),
                    new DataPoint(104, 12.5),
                    new DataPoint(108, 12.7),
                    new DataPoint(112, 12.9),
                    new DataPoint(116, 13.1),
                    new DataPoint(120, 13.3),
                    new DataPoint(124, 13.5),
                    new DataPoint(128, 13.7),
                    new DataPoint(132, 13.8),
                    new DataPoint(136, 14.0),
                    new DataPoint(140, 14.2),
                    new DataPoint(144, 14.3)
            });
        }
    }

    /**
     * \brief The high weight by gender
     * \param gender    The baby gender
     * \return The high weight values
     */
    private LineGraphSeries<DataPoint> getHighWeight(String gender) {
        if (gender.equals("girl")) {
            return new LineGraphSeries<>(new DataPoint[]{
                    new DataPoint(0, 4.8),
                    new DataPoint(1, 5.1),
                    new DataPoint(2, 5.4),
                    new DataPoint(3, 6.0),
                    new DataPoint(4, 6.4),
                    new DataPoint(5, 6.7),
                    new DataPoint(6, 6.9),
                    new DataPoint(7, 7.2),
                    new DataPoint(8, 7.4),
                    new DataPoint(9, 7.6),
                    new DataPoint(10, 7.8),
                    new DataPoint(11, 8.0),
                    new DataPoint(12, 8.2),
                    new DataPoint(13, 8.4),

                    new DataPoint(16, 9.3),
                    new DataPoint(20, 10.0),
                    new DataPoint(24, 10.6),
                    new DataPoint(28, 11.1),
                    new DataPoint(32, 11.6),
                    new DataPoint(36, 12.0),
                    new DataPoint(40, 12.4),
                    new DataPoint(44, 12.8),
                    new DataPoint(48, 13.1),
                    new DataPoint(52, 13.5),
                    new DataPoint(56, 13.8),
                    new DataPoint(60, 14.1),
                    new DataPoint(64, 14.5),
                    new DataPoint(68, 14.8),
                    new DataPoint(72, 15.1),
                    new DataPoint(76, 15.4),
                    new DataPoint(80, 15.7),
                    new DataPoint(84, 16.0),
                    new DataPoint(88, 16.4),
                    new DataPoint(92, 16.7),
                    new DataPoint(96, 17.0),
                    new DataPoint(100, 17.3),
                    new DataPoint(104, 17.7),
                    new DataPoint(108, 18.0),
                    new DataPoint(112, 18.3),
                    new DataPoint(116, 18.7),
                    new DataPoint(120, 19.0),
                    new DataPoint(124, 19.3),
                    new DataPoint(128, 19.6),
                    new DataPoint(132, 20.0),
                    new DataPoint(136, 20.3),
                    new DataPoint(140, 20.6),
                    new DataPoint(144, 20.9)
            });
        }
        else {
            return new LineGraphSeries<>(new DataPoint[]{
                    new DataPoint(0, 4.8),
                    new DataPoint(1, 5.1),
                    new DataPoint(2, 5.4),
                    new DataPoint(3, 5.7),
                    new DataPoint(4, 6.1),
                    new DataPoint(5, 6.5),
                    new DataPoint(6, 6.8),
                    new DataPoint(7, 7.1),
                    new DataPoint(8, 7.3),
                    new DataPoint(9, 7.6),
                    new DataPoint(10, 8.0),
                    new DataPoint(11, 8.4),
                    new DataPoint(12, 8.8),
                    new DataPoint(13, 9.1),

                    new DataPoint(16, 9.7),
                    new DataPoint(20, 10.4),
                    new DataPoint(24, 10.9),
                    new DataPoint(28, 11.4),
                    new DataPoint(32, 11.9),
                    new DataPoint(36, 12.3),
                    new DataPoint(40, 12.7),
                    new DataPoint(44, 13.0),
                    new DataPoint(48, 13.3),
                    new DataPoint(52, 13.7),
                    new DataPoint(56, 14.0),
                    new DataPoint(60, 14.3),
                    new DataPoint(64, 14.6),
                    new DataPoint(68, 14.9),
                    new DataPoint(72, 15.3),
                    new DataPoint(76, 15.6),
                    new DataPoint(80, 15.9),
                    new DataPoint(84, 16.2),
                    new DataPoint(88, 16.5),
                    new DataPoint(92, 16.8),
                    new DataPoint(96, 17.1),
                    new DataPoint(100, 17.5),
                    new DataPoint(104, 17.8),
                    new DataPoint(108, 18.1),
                    new DataPoint(112, 18.4),
                    new DataPoint(116, 18.7),
                    new DataPoint(120, 19.0),
                    new DataPoint(124, 19.3),
                    new DataPoint(128, 19.6),
                    new DataPoint(132, 19.9),
                    new DataPoint(136, 20.2),
                    new DataPoint(140, 20.4),
                    new DataPoint(144, 20.7)
            });
        }
    }

    /**
     * \brief Display the weight curve by gender
     * \param gender    The baby gender
     */
     public void displayWeightCurve(String gender){
        graph.removeAllSeries();
        customMyGraphs("kg   ");

         if(gender.equals("girl")){
             baby_gender.setBackgroundResource(R.drawable.girl);
         }
         else if(gender.equals("boy")){
             baby_gender.setBackgroundResource(R.drawable.boy);
         }
         else{
             baby_gender.setBackgroundResource(R.drawable.neutral);
         }

        LineGraphSeries<DataPoint> lowWeight= getLowWeight(gender);
        lowWeight.setCustomPaint(paint);

        LineGraphSeries<DataPoint> normalWeight = getNormalWeight(gender);
        normalWeight.setCustomPaint(paint2);

        LineGraphSeries<DataPoint> highWeight = getHighWeight(gender);
        highWeight.setCustomPaint(paint);

        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(144);
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(findMinValue(true));
        graph.getViewport().setMaxY(findMaxValue(true));
        graph.addSeries(lowWeight);
        graph.addSeries(normalWeight);
        graph.addSeries(highWeight);

        if(mWeight.size()<1) {
            mWeight.add(new DataPoint(0, 3.3));
        }

        serie.resetData(mWeight.toArray(new DataPoint[mWeight.size()]));
        graph.addSeries(serie);
        graph.getViewport().setScalable(true);
        graph.getViewport().getMaxX(true);
    }

    /**
     * \brief The low height by gender
     * \param gender    The baby gender
     * \return The low height weight values
     */
    private LineGraphSeries<DataPoint> getLowSize(String gender){
        if (gender.equals("girl")){
            return new LineGraphSeries<>(new DataPoint[] {
                    new DataPoint(0, 43.6),
                    new DataPoint(1, 44.7),
                    new DataPoint(2, 45.8),
                    new DataPoint(3, 46.7),
                    new DataPoint(4, 47.5),
                    new DataPoint(5, 48.3),
                    new DataPoint(6, 49.1),
                    new DataPoint(7, 49.8),
                    new DataPoint(8, 50.5),
                    new DataPoint(9, 51.2),
                    new DataPoint(10, 51.8),
                    new DataPoint(11, 52.4),
                    new DataPoint(12, 52.9),
                    new DataPoint(13, 53.5),

                    new DataPoint(16, 55.6),
                    new DataPoint(20, 57.4),
                    new DataPoint(24, 58.9),
                    new DataPoint(28, 60.3),
                    new DataPoint(32, 61.7),
                    new DataPoint(36, 62.9),
                    new DataPoint(40, 64.1),
                    new DataPoint(44, 65.2),
                    new DataPoint(48, 66.3),
                    new DataPoint(52, 67.3),
                    new DataPoint(56, 68.3),
                    new DataPoint(60, 69.3),
                    new DataPoint(64, 70.2),
                    new DataPoint(68, 71.1),
                    new DataPoint(72, 72.0),
                    new DataPoint(76, 72.8),
                    new DataPoint(80, 73.7),
                    new DataPoint(84, 74.5),
                    new DataPoint(88, 75.2),
                    new DataPoint(92, 76.0),
                    new DataPoint(96, 76.4),
                    new DataPoint(100, 76.8),
                    new DataPoint(104, 77.5),
                    new DataPoint(108, 78.1),
                    new DataPoint(112, 78.8),
                    new DataPoint(116, 79.5),
                    new DataPoint(120, 80.1),
                    new DataPoint(124, 80.7),
                    new DataPoint(128, 81.3),
                    new DataPoint(132, 81.9),
                    new DataPoint(136, 82.5),
                    new DataPoint(140, 83.1),
                    new DataPoint(144, 83.6)
            });
        }
        else{
            return new LineGraphSeries<>(new DataPoint[] {
                    new DataPoint(0, 44.2),
                    new DataPoint(1, 45.4),
                    new DataPoint(2, 46.6),
                    new DataPoint(3, 47.6),
                    new DataPoint(4, 48.6),
                    new DataPoint(5, 49.5),
                    new DataPoint(6, 50.3),
                    new DataPoint(7, 51.1),
                    new DataPoint(8, 51.9),
                    new DataPoint(9, 52.6),
                    new DataPoint(10, 53.3),
                    new DataPoint(11, 54.0),
                    new DataPoint(12, 54.7),
                    new DataPoint(13, 55.3),

                    new DataPoint(16, 57.6),
                    new DataPoint(20, 59.6),
                    new DataPoint(24, 61.2),
                    new DataPoint(28, 62.7),
                    new DataPoint(32, 64.0),
                    new DataPoint(36, 65.2),
                    new DataPoint(40, 66.4),
                    new DataPoint(44, 67.6),
                    new DataPoint(48, 68.6),
                    new DataPoint(52, 69.6),
                    new DataPoint(56, 70.6),
                    new DataPoint(60, 71.6),
                    new DataPoint(64, 72.5),
                    new DataPoint(68, 73.3),
                    new DataPoint(72, 74.2),
                    new DataPoint(76, 75.0),
                    new DataPoint(80, 75.8),
                    new DataPoint(84, 76.5),
                    new DataPoint(88, 77.2),
                    new DataPoint(92, 78.0),
                    new DataPoint(96, 78.7),
                    new DataPoint(100, 78.6),
                    new DataPoint(104, 79.3),
                    new DataPoint(108, 79.9),
                    new DataPoint(112, 80.5),
                    new DataPoint(116, 81.1),
                    new DataPoint(120, 81.7),
                    new DataPoint(124, 82.3),
                    new DataPoint(128, 82.8),
                    new DataPoint(132, 83.4),
                    new DataPoint(136, 83.9),
                    new DataPoint(140, 84.4),
                    new DataPoint(144, 85.0)
            });
        }
    }

    /**
     * \brief The normal height by gender
     * \param gender    The baby gender
     * \return The normal height weight values
     */
    private LineGraphSeries<DataPoint> getNormalSize(String gender){
        if (gender.equals("girl")){
            return new LineGraphSeries<>(new DataPoint[] {
                new DataPoint(0, 49.1),
                new DataPoint(1, 50.3),
                new DataPoint(2, 51.5),
                new DataPoint(3, 52.5),
                new DataPoint(4, 53.4),
                new DataPoint(5, 54.2),
                new DataPoint(6, 55.1),
                new DataPoint(7, 55.8),
                new DataPoint(8, 56.6),
                new DataPoint(9, 57.3),
                new DataPoint(10, 57.9),
                new DataPoint(11, 58.6),
                new DataPoint(12, 59.2),
                new DataPoint(13, 59.8),

                new DataPoint(16, 62.1),
                new DataPoint(20, 64.0),
                new DataPoint(24, 65.7),
                new DataPoint(28, 67.3),
                new DataPoint(32, 68.7),
                new DataPoint(36, 70.1),
                new DataPoint(40, 71.5),
                new DataPoint(44, 72.8),
                new DataPoint(48, 74.0),
                new DataPoint(52, 75.2),
                new DataPoint(56, 76.4),
                new DataPoint(60, 77.5),
                new DataPoint(64, 78.6),
                new DataPoint(68, 79.7),
                new DataPoint(72, 80.7),
                new DataPoint(76, 81.7),
                new DataPoint(80, 82.7),
                new DataPoint(84, 83.7),
                new DataPoint(88, 84.6),
                new DataPoint(92, 85.5),
                new DataPoint(96, 86.3),
                new DataPoint(100, 86.6),
                new DataPoint(104, 87.4),
                new DataPoint(108, 88.3),
                new DataPoint(112, 89.1),
                new DataPoint(116, 89.9),
                new DataPoint(120, 90.7),
                new DataPoint(124, 91.4),
                new DataPoint(128, 92.2),
                new DataPoint(132, 92.9),
                new DataPoint(136, 93.6),
                new DataPoint(140, 94.4),
                new DataPoint(144, 95.1)
        });
        }
        else{
            return new LineGraphSeries<>(new DataPoint[] {
                    new DataPoint(0, 49.9),
                    new DataPoint(1, 51.1),
                    new DataPoint(2, 52.3),
                    new DataPoint(3, 53.4),
                    new DataPoint(4, 54.4),
                    new DataPoint(5, 55.3),
                    new DataPoint(6, 56.2),
                    new DataPoint(7, 57.1),
                    new DataPoint(8, 57.9),
                    new DataPoint(9, 58.7),
                    new DataPoint(10, 59.4),
                    new DataPoint(11, 60.1),
                    new DataPoint(12, 60.8),
                    new DataPoint(13, 61.4),

                    new DataPoint(16, 63.9),
                    new DataPoint(20, 65.9),
                    new DataPoint(24, 67.6),
                    new DataPoint(28, 69.2),
                    new DataPoint(32, 70.6),
                    new DataPoint(36, 72.0),
                    new DataPoint(40, 73.3),
                    new DataPoint(44, 74.5),
                    new DataPoint(48, 75.7),
                    new DataPoint(52, 76.9),
                    new DataPoint(56, 78.0),
                    new DataPoint(60, 79.1),
                    new DataPoint(64, 80.2),
                    new DataPoint(68, 81.2),
                    new DataPoint(72, 82.3),
                    new DataPoint(76, 83.2),
                    new DataPoint(80, 84.2),
                    new DataPoint(84, 85.1),
                    new DataPoint(88, 86.0),
                    new DataPoint(92, 86.9),
                    new DataPoint(96, 87.8),
                    new DataPoint(100, 88.0),
                    new DataPoint(104, 88.8),
                    new DataPoint(108, 89.6),
                    new DataPoint(112, 90.4),
                    new DataPoint(116, 91.2),
                    new DataPoint(120, 92.7),
                    new DataPoint(124, 93.4),
                    new DataPoint(128, 94.1),
                    new DataPoint(132, 94.8),
                    new DataPoint(136, 95.4),
                    new DataPoint(140, 96.1),
                    new DataPoint(144, 96.5)
            });
        }
    }

    /**
     * \brief The high height by gender
     * \param gender    The baby gender
     * \return The high height weight values
     */
    private LineGraphSeries<DataPoint> getHighSize(String gender){
        if (gender.equals("girl")){
            return new LineGraphSeries<>(new DataPoint[] {
                    new DataPoint(0, 54.7),
                    new DataPoint(1, 56.0),
                    new DataPoint(2, 57.2),
                    new DataPoint(3, 58.2),
                    new DataPoint(4, 59.2),
                    new DataPoint(5, 60.1),
                    new DataPoint(6, 61.0),
                    new DataPoint(7, 61.9),
                    new DataPoint(8, 62.6),
                    new DataPoint(9, 63.4),
                    new DataPoint(10, 64.1),
                    new DataPoint(11, 64.8),
                    new DataPoint(12, 65.5),
                    new DataPoint(13, 66.1),

                    new DataPoint(16, 68.6),
                    new DataPoint(20, 70.7),
                    new DataPoint(24, 72.5),
                    new DataPoint(28, 74.2),
                    new DataPoint(32, 75.8),
                    new DataPoint(36, 77.4),
                    new DataPoint(40, 78.9),
                    new DataPoint(44, 80.3),
                    new DataPoint(48, 81.7),
                    new DataPoint(52, 83.1),
                    new DataPoint(56, 84.4),
                    new DataPoint(60, 85.7),
                    new DataPoint(64, 87.0),
                    new DataPoint(68, 88.2),
                    new DataPoint(72, 89.4),
                    new DataPoint(76, 90.6),
                    new DataPoint(80, 91.7),
                    new DataPoint(84, 92.9),
                    new DataPoint(88, 94.0),
                    new DataPoint(92, 95.0),
                    new DataPoint(96, 95.4),
                    new DataPoint(100, 96.4),
                    new DataPoint(104, 97.4),
                    new DataPoint(108, 98.4),
                    new DataPoint(112, 99.4),
                    new DataPoint(116, 100.3),
                    new DataPoint(120, 101.3),
                    new DataPoint(124, 102.3),
                    new DataPoint(128, 103.1),
                    new DataPoint(132, 103.9),
                    new DataPoint(136, 104.8),
                    new DataPoint(140, 105.6),
                    new DataPoint(144, 106.5)
            });
        }
        else{
            return new LineGraphSeries<>(new DataPoint[] {
                    new DataPoint(0, 55.6),
                    new DataPoint(1, 56.8),
                    new DataPoint(2, 58.1),
                    new DataPoint(3, 59.2),
                    new DataPoint(4, 60.2),
                    new DataPoint(5, 61.2),
                    new DataPoint(6, 62.1),
                    new DataPoint(7, 63.0),
                    new DataPoint(8, 63.9),
                    new DataPoint(9, 64.7),
                    new DataPoint(10, 65.4),
                    new DataPoint(11, 66.2),
                    new DataPoint(12, 66.9),
                    new DataPoint(13, 67.5),

                    new DataPoint(16, 70.1),
                    new DataPoint(20, 72.2),
                    new DataPoint(24, 74.0),
                    new DataPoint(28, 75.7),
                    new DataPoint(32, 77.2),
                    new DataPoint(36, 78.7),
                    new DataPoint(40, 80.1),
                    new DataPoint(44, 81.5),
                    new DataPoint(48, 82.9),
                    new DataPoint(52, 84.2),
                    new DataPoint(56, 85.5),
                    new DataPoint(60, 86.7),
                    new DataPoint(64, 88.0),
                    new DataPoint(68, 89.2),
                    new DataPoint(72, 90.4),
                    new DataPoint(76, 91.5),
                    new DataPoint(80, 92.6),
                    new DataPoint(84, 93.8),
                    new DataPoint(88, 94.9),
                    new DataPoint(92, 95.9),
                    new DataPoint(96, 97.0),
                    new DataPoint(100, 97.3),
                    new DataPoint(104, 98.3),
                    new DataPoint(108, 99.3),
                    new DataPoint(112, 100.3),
                    new DataPoint(116, 101.2),
                    new DataPoint(120, 102.1),
                    new DataPoint(124, 103.0),
                    new DataPoint(128, 103.9),
                    new DataPoint(132, 104.8),
                    new DataPoint(136, 105.6),
                    new DataPoint(140, 106.4),
                    new DataPoint(144, 107.2)
            });
        }
    }

    /**
     * \brief Display the height curve by gender
     * \param gender    The baby gender
     */
    public void displaySizeCurve(String gender){
        graph.removeAllSeries();
        customMyGraphs("cm   ");

        if(gender.equals("girl")){
            baby_gender.setBackgroundResource(R.drawable.girl);
        }
        else if(gender.equals("boy")){
            baby_gender.setBackgroundResource(R.drawable.boy);
        }
        else{
            baby_gender.setBackgroundResource(R.drawable.neutral);
        }

        LineGraphSeries<DataPoint> lowSize= getLowSize(gender);
        lowSize.setCustomPaint(paint);
        LineGraphSeries<DataPoint> normalSize = getNormalSize(gender);
        normalSize.setCustomPaint(paint2);

        LineGraphSeries<DataPoint> highSize = getHighSize(gender);
        highSize.setCustomPaint(paint);

        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setMinX(0);
        graph.getViewport().setMaxX(144);
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(findMinValue(false));
        graph.getViewport().setMaxY(findMaxValue(false));
        graph.getViewport().setScalable(true);
        graph.addSeries(lowSize);
        graph.addSeries(normalSize);
        graph.addSeries(highSize);

        if(mSize.size()<1) {
            mSize.add(new DataPoint(0, 49.5));
        }
        serie.resetData(mSize.toArray(new DataPoint[mSize.size()]));
        graph.addSeries(serie);
    }

    /**
     * \brief Find the smallest value of the array list
     * \param isWeight  True if it is the weight curve
     * \return The smallest value
     */
    private double findMinValue(boolean isWeight){
        double minValue;
        if(isWeight){
            minValue = 2;
            for(int i = 0 ; i<mWeight.size() ; i++){
                if(mWeight.get(i).getY() < minValue){
                    minValue = mWeight.get(i).getY();
                }
            }
        }
        else{
            minValue = 44;
            for(int i = 0 ; i<mSize.size() ; i++){
                if(mSize.get(i).getY() < minValue){
                    minValue = mSize.get(i).getY();
                }
            }
        }
        return minValue;
    }

    /**
     * \brief Find the biggest value of the array list
     * \param isWeight  True if it is the weight curve
     * \return The biggest value
     */
    private double findMaxValue(boolean isWeight){
        double maxValue;
        if(isWeight){
            maxValue = 21;
            for (int i = 0; i < mWeight.size(); i++) {
                if (mWeight.get(i).getY() > maxValue) {
                    maxValue = mWeight.get(i).getY();
                }
            }
        }
        else{
            maxValue = 108;
            for (int i = 0; i < mSize.size(); i++) {
                if (mSize.get(i).getY() > maxValue) {
                    maxValue = mSize.get(i).getY();
                }
            }
        }
        return maxValue;
    }

    /**
     * \brief Show the view
     * \param thresholdTemp The value of temperature threshold
     * \param thresholdHum  The value of humidity threshold
     */
    public void display(int thresholdTemp, int thresholdHum){
        getTempButton().setBackgroundResource(getTempDrawable()[thresholdTemp]);
        getHumidityButton().setBackgroundResource(getHumDrawable()[thresholdHum]);
        getHealthButton().setBackgroundResource(R.drawable.health_active);
    }

    /**
     * \brief Close the view (empty)
     */
    public void close(){
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the add button
     */
    public Button getAdd_button() {
        return add_button;
    }

    /**
     * \brief Get method for button to change the curve
     */
    public ImageButton getChangeCurve_button() {
        return changeCurve_button;
    }

    /**
     * \brief Get method for age up button
     */
    public ImageButton getAge_up() {
        return age_up;
    }

    /**
     * \brief Get method for the unit down button
     */
    public ImageButton getUnit_down() {
        return unit_down;
    }

    /**
     * \brief Get method for the age down button
     */
    public ImageButton getAge_down() {
        return age_down;
    }

    /**
     * \brief Get method for the warning neutral
     */
    public TextView getWarning_neutral() {
        return warning_neutral;
    }

    /**
     * \brief Get method for the unit up button
     */
    public ImageButton getUnit_up() {
        return unit_up;
    }

    /**
     * \brief Get method for the data unit
     */
    public TextView getData_unit() {
        return data_unit;
    }

    /**
     * \brief Get method for the age
     */
    public TextView getAge() {
        return age;
    }

    /**
     * \brief Get method for the unit
     */
    public TextView getUnit() {
        return unit;
    }

    /**
     * \brief Get method for the data
     */
    public TextView getData() {
        return data;
    }

    /**
     * \brief Get method for the edit text
     */
    public EditText getData_text() {
        return data_text;
    }

    /**
     * \brief Get method for the title
     */
    public TextView getTitle() {
        return title;
    }

    /**
     * \brief Get method to know if is the weight curve
     */
    public boolean isWeight() {
        return isWeight;
    }

    /**
     * \brief Get method for the list of weight
     */
    public ArrayList<DataPoint> getmWeight() {
        return mWeight;
    }

    /**
     * \brief Get method for the list of height
     */
    public ArrayList<DataPoint> getmSize() {
        return mSize;
    }

    /**
     * \brief Get method for all the datas
     */
    public LineGraphSeries<DataPoint> getSerie() {
        return serie;
    }

    /**
     * \brief Set method for the add button
     */
    public void setAdd_button(Button add_button) {
        this.add_button = add_button;
    }

    /**
     * \brief Set method for the button to change the curve
     */
    public void setChangeCurve_button(ImageButton changeCurve_button) {
        this.changeCurve_button = changeCurve_button;
    }

    /**
     * \brief Set method for age up button
     */
    public void setAge_up(ImageButton age_up) {
        this.age_up = age_up;
    }

    /**
     * \brief Set method for the age down button
     */
    public void setAge_down(ImageButton age_down) {
        this.age_down = age_down;
    }

    /**
     * \brief Set method for the unit up button
     */
    public void setUnit_up(ImageButton unit_up) {
        this.unit_up = unit_up;
    }

    /**
     * \brief Set method for the unit down button
     */
    public void setUnit_down(ImageButton unit_down) {
        this.unit_down = unit_down;
    }

    /**
     * \brief Set method for the data unit
     */
    public void setData_unit(TextView data_unit) {
        this.data_unit = data_unit;
    }

    /**
     * \brief Set method for the age
     */
    public void setAge(TextView age) {
        this.age = age;
    }

    /**
     * \brief Set method for the unit
     */
    public void setUnit(TextView unit) {
        this.unit = unit;
    }

    /**
     * \brief Set method for the edit text
     */
    public void setData_text(EditText data_text) {
        this.data_text = data_text;
    }

    /**
     * \brief Set method for the data
     */
    public void setData(TextView data) {
        this.data = data;
    }

    /**
     * \brief Set method for the graphView class
     */
    public void setGraph(GraphView graph) {
        this.graph = graph;
    }

    /**
     * \brief Set method for the title
     */
    public void setTitle(TextView title) {
        this.title = title;
    }

    /**
     * \brief Set method to change if it is a weight curve
     */
    public void setWeight(boolean isWeight) {
        this.isWeight = isWeight;
    }

    /**
     * \brief Set method for the weight data
     */
    public void setmWeight(ArrayList<DataPoint> mWeight) {
        this.mWeight = mWeight;
    }

    /**
     * \brief Set method for the height data
     */
    public void setmSize(ArrayList<DataPoint> mSize) {
        this.mSize = mSize;
    }

    /**
     * \brief Set method for the picture of baby
     */
    public void setBaby_gender(ImageView baby_gender) {
        this.baby_gender = baby_gender;
    }

    /**
     * \brief Set method for the warning neutral
     */
    public void setWarning_neutral(TextView warning_neutral) {
        this.warning_neutral = warning_neutral;
    }
}